package com.example.radioplayer;


import java.util.ArrayList;
import java.util.List;

import com.example.radioplayer.MusicLoader.Ablum;
import com.example.radioplayer.MusicLoader.MusicLibary;
import com.example.radioplayer.RadioPlayerService.MusicBinder;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;

@SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
	private ArrayList<Fragment> fragments;
	private MusicLibary musicLibary;
	private ViewPager radioPager;
	private MusicBinder mBinder;
	ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			// TODO Auto-generated method stub

		}
        //绑定musicService时获得bind和Service通信
		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			// TODO Auto-generated method stub
			mBinder = (MusicBinder) arg1;

		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dashboard);
		MusicLoader m = MusicLoader.instance(getContentResolver());
		musicLibary = m.getMusicLibary();
		Intent intent = new Intent();
		intent.setClass(this, RadioPlayerService.class);
		bindService(intent, conn, Context.BIND_AUTO_CREATE);
		Log.d("lsj", m.getMusicList().get(0).musics.toString());
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		fragments = new ArrayList<Fragment>();
    	List<Ablum> ablums = musicLibary.ablums;
    	
    	for(Ablum ablum : ablums){
    		fragments.add(new RadioFragment(ablum));
    	}
	
    	radioPager = (ViewPager) findViewById(R.id.pager_radio);
    	radioPager.setAdapter(new RadioPagerAdapter(this.getSupportFragmentManager()));
    	radioPager.setOnPageChangeListener(new RadioPageChangeListener());
	
    	/*动态添加下方小圆点*/
    	addDots(fragments.size());
    	setDotsSelected(0);
	}

	/**
	 * 自定义ViewPagerAdapter
	 */
	
	private class RadioPagerAdapter extends FragmentPagerAdapter {

		public RadioPagerAdapter(FragmentManager fm) {
			super(fm);
		}
		
		@Override
		public int getCount() {
			return fragments.size();
		}
		
		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}
	}
	
	/**
	 * 自定义PageChangeListener
	 */
	private class RadioPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int position) {
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			
		}

		@Override
		public void onPageSelected(int position) {
			mBinder.setIsplay(true);
			((RadioFragment)fragments.get(position)).changeMusicStatus();
			setDotsSelected(position);
			mBinder.toLocalAblum(position);
		}
	}

	private void addDots(int num) {
		LinearLayout mDotsLayout = (LinearLayout) findViewById(R.id.layout_dots);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_point_grey);
		for(int i = 0; i < num; i++) {
			ImageView img = new ImageView(this);
			img.setLayoutParams(new LinearLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight()));
			img.setImageResource(R.drawable.ic_point_grey);
			LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) img.getLayoutParams();
			lp.setMargins(5, 0, 5, 0);
			img.setLayoutParams(lp);
			mDotsLayout.addView(img);
	 	}
	}
	 
	/**
	 * 设置第几个小圆圈被选中
	 * 参数: position
	 */
	private void setDotsSelected(int position) {
		LinearLayout mDotsLayout = (LinearLayout) findViewById(R.id.layout_dots);
		for(int i = 0; i < mDotsLayout.getChildCount(); i++) {
			if(i == position)
				((ImageView) mDotsLayout.getChildAt(i)).setImageResource(R.drawable.ic_point_red);
			else
				((ImageView) mDotsLayout.getChildAt(i)).setImageResource(R.drawable.ic_point_grey);
		}
	}
	
	public MusicBinder getBinder(){
		return mBinder;
	}

}